package com.mi.proxy4;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Test {

	public static void main(String[] args) throws Exception {
		Class<?>[] interfaces = new Class<?>[]{Singer.class,Person.class};
		Object proxy = Proxy.newProxyInstance(Test.class.getClassLoader(),interfaces, new MyProxyInvocation());
		System.out.println(proxy instanceof Singer && proxy instanceof Person);
		Singer singer = (Singer)proxy;
		Person person = (Person)proxy;
		singer.sing("小苹果");
		System.out.println(person.lives());
		System.out.println(proxy.getClass().getName());
		Object proxy2 = Proxy.newProxyInstance(Test.class.getClassLoader(), new Class<?>[]{Person.class}, new InvocationHandler() {
			
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				return null;
			}
		});
		System.out.println(proxy2.getClass().getName());
	}

}


class MyProxyInvocation implements InvocationHandler{

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		String methodName = method.getName();
		if(methodName.equals("sing")){
			String songName = (String)args[0];
			System.out.println("我不会唱" + songName + "我只是一个码农而已,我只会唱“世上只有妈妈好，有妈的孩子像个宝！”");
			return null;
		}
		
		if(methodName.equals("sign")){
			System.out.println("今天终于找到工作啦，和一家软件公司签约啦！奶奶的，后来才知道原来是培训机构啊！！");
			return null;
		}
		
		if(methodName.equals("work")){
			System.out.println("暂时还在工地搬砖呢");
			return null;
		}
		
		if(methodName.equals("lives")){
			System.out.println("单身狗一个，毫无幸福感可言！");
			return 0;
		}
		
		return null;
	}
	
}